var plib = require("./plib");
var plog = require("./plog");
//先进先出的 文本 buff 的一个封装
var pBuffer = function()
{
	this.data = {};
	this.data._array = {}; //new Array();
	this.data._now = 0;
	this.data._size = 100;
}
pBuffer.prototype.Add = function(string)
{
	plog.out("now is " + this.data._now);
	plog.out("string is " + string);
	this.data._array[this.data._now] = string;
	this.data._now ++;
	if(this.data._now >= this.data._size)
		this.data._now = 0;
}

pBuffer.prototype.GetArray = function(count)
{
	var ret = new Array();
	var index = 0; 
	//plog.out("fuck the baby");
	for(var i = this.data._now; i < this.data._size; i ++)
	{
		//plog.out("i is " + i);
		if(i in this.data._array)
		{
			ret[index] = this.data._array[i];
			//plog.out("index" + index + " is " + this.data._array[i]);
			index ++;
		}
	}
	for(var i = 0; i < this.data._now; i ++)
	{
		//plog.out("i is " + i);
		if(i in this.data._array)
		{
			ret[index] = this.data._array[i];
			//plog.out("index" + index + " is " + this.data._array[i]);
			index ++;
		}
	}
	var start = ret.length - count; 
	var ret2 = new Array();
	var index = 0;
	for(var i = start; i < ret.length; i ++)
	{
		if(i in ret){
		ret2[index] = ret[i];
		index++;}
	}
	return ret2;
}

pBuffer.prototype.GetString = function(count)
{
	if(count == undefined) count = this.data._size;
	var k = this.GetArray(count);
	var ret = "";
	for(var i = 0; i < k.length; i ++)
	{
		if(i != 0) 
			ret = ret + "\n";
		ret = ret + k[i];
	}
	return ret;
}
pBuffer.prototype.ToString = function()
{
	return plib.obj2str(this.data);
}

module.exports.str2obj = function(str)
{
	var a = new pBuffer();
	a.data = plib.str2obj(str);
	return a;

}
module.exports.Create = function(size)
{
	if(size == undefined) size = 100;
	
	var a = new pBuffer();
	a.data._size = size;
	return a;
};
